<?php
class daka_class_catModel extends Model
{
	public $_validate=array(
		array('name','require','分类名称不能为空'),
		//array('img','require','分类图片不能为空'),
		array('name','','分类名称已经存在',0,'unique'),
	);
	public $cache_name = 'class/list';
	/**
     * 后台有更新则删除缓存
     */
    protected function _before_write($data, $options) {
        F('class/list', NULL);
		F('teacher/list', NULL);
        F('cat/list', NULL);
    }
    protected function _after_delete($data,$options) {
        F('class/list', NULL);
        F('teacher/list', NULL);
        F('cat/list', NULL);
    }
    public function set_cache() {
		$map['status'] = 1;
        //判断这个级别是否开启
        $list = $this->field('id,name,ordid')->where($map)->order('ordid asc')->select();
        if(!$list){
            $list = 0;
        }else{
			foreach($list as $k => $v){
				$cl['status'] = 1;
				$cl['cat_id'] = $v['id'];
				$cl['id'] = array('neq',C('zyb_rookies_id'));
				
				$class = D('daka_class')->field('id,cname as name,ordid,content as img')->where($cl)->order('ordid asc')->select();
				if(!$class){
					$list[$k]['class'] = array();
				}else{
					
					//dump($class);
					//找出属于这个课堂的 所有讲座
					foreach($class as $c => $d){
						$d['img'] = get_img_url($d['img']);
						$d['url'] = C('zyb_site_url').'list.html#id='.$d['id'];
						$list[$k]['class'][] = $d;
						$lis['status'] = 1;
						$lis['class_id'] = $d['id'];
						$lis['type'] = 1;
						
						$lis_data = D('daka_class_list')->field('id,title,img,uid,read_num,share_num,ordid,is_hot,content')->where($lis)->order('is_hot desc,ordid asc')->relation(true)->limit(3)->select();
						if(!$lis_data){
							$list[$k]['class'][$c]['class_list'] = array();
						}else{
							//$list[$k]['class'][$c]['class_list'] = $lis_data;
							foreach($lis_data as $q => $t){
								$tmp['id'] = $t['id'];
								$tmp['title'] = $t['title'];
								$tmp['img'] = get_img_url($t['img']);
								$tmp['read_num'] = $t['read_num'];
								$tmp['share_num'] = $t['share_num'];
								$tmp['uname'] = $t['daka_user']['uname'];
								$tmp['ordid'] = $t['ordid'];
								$tmp['is_hot'] = $t['is_hot'];
								$tmp['url'] = $t['content'];
								$list[$k]['class'][$c]['class_list'][] = $tmp;
							}
						}
					}
				}
			}
			F('cat/list',$list);
        }
    }

    /**
     * 生成spid
     *
     * @param int $pid 父级ID
     */
    public function get_spid($pid) {
        if (!$pid) {
            return 0;
        }
        $pspid = $this->where(array('id'=>$pid))->getField('spid');
        if ($pspid) {
            $spid = $pspid . $pid . '|';
        } else {
            $spid = $pid . '|';
        }
        return $spid;
    }
   //获取栏目分类
    public function get_var($var,$show=0){
    	if(F('item_cate_list')===false){
    		$list=$this->field('id,name,variable,pid,img,show,ordid')->order('ordid asc,id asc')->select();
    		F('item_cate_list',$list);
    	}
    	$pid='';
    	foreach(F('item_cate_list') as $k=>$v){
    		 if($v['variable']==$var){
    		 	$pid=$v['id']; break;
    		 }
    	}
    	$arr=array();
    	$i=1;
    	foreach(F('item_cate_list') as $k=>$v){
    		  if($v['pid']==$pid&&($show?$v['show']==1:1)){
    		 	$arr[$i]=$v;
    		 	$i++;
    		 }
    	}
    	return $arr;
    }

    //循环变量输出
    public function gat_var_list($var,$id='id',$name='name'){
    	$list=$arr=array();
    	$arr=D('item_cate')->get_var($var);
    	foreach($arr as $v){
    		$list[$v[$id]]=$v[$name];
    	}
    	return $list;
    }

    public function get_list($id='id',$name='name'){
    	if(F('item_cate_list')===false){
    	    $list=$this->field('id,name,variable,pid,img,show,ordid')->order('ordid asc')->select();
    		F('item_cate_list',$list);
    	}
        $list=array();
    	foreach(F('item_cate_list') as $v){
                $list[$v[$id]]=$v[$name];
    	}
    	return $list;
    }


    /**
     * 获取分类下面的所有子分类的ID集合
     *
     * @param int $id
     * @param bool $with_self
     * @return array $array
     */
    public function get_child_ids($id, $with_self=false) {
        $spid = $this->where(array('id'=>$id))->getField('spid');
        $spid = $spid ? $spid .= $id .'|' : $id .'|';
        $id_arr = $this->field('id')->where(array('spid'=>array('like', $spid.'%')))->select();
        $array = array();
        foreach ($id_arr as $val) {
            $array[] = $val['id'];
        }
        $with_self && $array[] = $id;
        return $array;
    }

  /**
     * 获取分类下面的所有子分类的常用数据集合
     * 只获取前5级分类
     * @param int $id
     * @return array $array
     */
    public function get_child_datas($id) {
        $data=$this->_getlistofdata($id,1);
        $array=array();
        foreach($data as $v){  //第二级
        	$v['az']=$this->_getFirstCharter($v['variable']);
        	if($v['show']==1){
        		$array['hot'][$v['id']]=$v; //热门
        	}
        	$v['sub']=$this->_getlistofdata($v['id']);
            if($v['sub']){  //第三级
            	foreach($v['sub'] as $kk=>$vv){
                  $v['sub'][$kk]['bsub']=$this->_getlistofdata($vv['id']);
            	  if($v['sub'][$kk]['bsub']){   //第四级
                        foreach($v['sub'][$kk]['bsub'] as $m=>$n){
                           $v['sub'][$kk]['bsub'][$m]['year']= $this->_getlistofdata($n['id']);
                           if($v['sub'][$kk]['bsub'][$m]['year']){  //第五级
                                foreach($v['sub'][$kk]['bsub'][$m]['year'] as $e=>$r){
                                    $v['sub'][$kk]['bsub'][$m]['year'][$e]['type']=$this->_getlistofdata($r['id']);
                                }
                           }
                        }
            	   }
            	}
            }
            $array[$this->_getFirstCharter($v['variable'])][$v['id']]=$v;
        }
        return $array;
    }

    public function get_parent_ids($id){
        $spid = $this->where(array('id'=>$id))->getField('spid');
        $spid = $spid ? $spid .= $id : $id ;
        $arr=explode('|',$spid);
        foreach($arr as $k=>$v){
        	$temp=$this->where(array('id'=>$v))->field('id,name')->select();
        	$str[$k]=$temp[0];
        }
        return $str;

    }

    /**
     * 检测分类是否存在
     *
     * @param string $name
     * @param int $pid
     * @param int $id
     * @return bool
     */
    public function name_exists($name, $pid, $id=0) {
        $where = "name='" . $name . "' AND pid='" . $pid . "' AND id<>'" . $id . "'";
        $result = $this->where($where)->count('id');
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
	 /**
	 *@todo 获取对应表数
	 *
	 */
	 public function _getlistofdata($pid,$flag=2){
	 	if($flag==1){
	 		$field='id,name,show,img,variable';
	 	}else{
            $field='id,name';
	 	}
        return $this->field($field)->where(array('pid'=>$pid,'status'=>1))->order('ordid asc,id asc')->select();
	 }

	  /**
     * 取汉字的第一个字的首字母
     * @param type $str
     * @return string|null
     */
	public  function _getFirstCharter($str){
	 	    $str=trim($str);
			return strtoupper($str{0});
	}
	
	

}